<template>
  <div class="allCity">
    <article ref="article" @scroll="onScroll">
      <header>
        <div class="gps">
          <h3>GPS定位你所在的城市</h3>
          <span>定位失败</span>
        </div>
        <div class="hot-city">
          <h3>热门城市</h3>
          <p>
            <span v-for="item in hotCity" :key="item.cityId">{{ item.name }}</span>
          </p>
        </div>
      </header>
      <main>
        <div class="type" v-for="(item) in list" :key="item.type">
          <h3 ref="h3">{{ item.type }}</h3>
          <p v-for="(item) in item.children" :key="item.cityId">{{ item.name }}</p>
        </div>
      </main>
    </article>
    <aside>
      <p
        v-for="(item, index) in list"
        :key="item.type"
        @click="toPos(index)"
        :class="{ active: curIndex === index }"
      >{{ item.type }}</p>
    </aside>
    <transition name="fade">
      <div class="tips" v-if="show">{{ text }}</div>
    </transition>
  </div>
</template>

<script>
export default {
  props: ['list', 'originList'],
  data () {
    return {
      curIndex: 0,
      text: '',
      show: false
    }
  },
  methods: {
    toPos (index) {
      // console.log(this.$refs.h3)
      this.show = true
      this.text = this.list[index].type
      this.$refs.article.scrollTop = this.$refs.h3[index].offsetTop
      if (this.timerId) clearTimeout(this.timerId)
      this.timerId = setTimeout(() => {
        this.show = false
        this.timerId = null
      }, 1500)
    },
    onScroll (e) {
      const scrollTop = e.target.scrollTop
      // console.log(scrollTop)
      const tops = this.$refs.h3.map(v => v.offsetTop).reverse()
      // console.log(tops)
      tops.some((v, i) => {
        // console.log(scrollTop, v)
        if (scrollTop + 30 > v) {
          this.curIndex = tops.length - 1 - i
          return true
        } else {
          return false
        }
      })
    }
  },
  computed: {
    hotCity () {
      if (!this.originList || !this.originList.length) return []
      return this.originList.filter(f => f.isHot)
    }
  }
}
</script>

<style lang="scss" scoped>
.allCity {
  overflow: hidden;
  display: flex;
  flex: 1;
  border-top: 0.01rem solid #fff;

  article {
    flex: 1;
    overflow: hidden;
    overflow-y: auto;
    position: relative;
    scroll-behavior: smooth;

    &::-webkit-scrollbar {
      display: none;
    }

    header {
      // height: 2.1rem;
      background: #fff;
      padding: 0.15rem 0 0 0.15rem;
      display: flex;
      flex-direction: column;

      h3 {
        height: 0.195rem;
        margin-bottom: 0.1rem;
        color: #797d82;
        font-size: 0.12rem;
      }

      p {
        display: flex;
        flex-wrap: wrap;
      }

      span {
        padding: 0.05rem 0.2rem;
        background: #f4f4f4;
        font-size: 0.14rem;
        border-radius: 0.04rem;
      }

      .gps {
        height: 0.745rem;
      }
      .hot-city {
        // min-height: 1.195rem;
        flex: 1;

        span {
          margin: 0 0.1rem 0.1rem 0;
        }
      }
    }

    main {
      h3 {
        padding-left: 0.15rem;
        height: 0.3rem;
        line-height: 0.3rem;
        font-size: 0.12rem;
        font-weight: normal;
        color: #797d82;
      }

      p {
        background: #fff;
        margin-bottom: 0.01rem;
        line-height: 0.48rem;
        padding-left: 0.2rem;
        font-size: 0.14rem;

        &:hover {
          background: #eee;
        }
      }
    }
  }

  aside {
    width: 0.18rem;
    background: #fff;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 0.12rem;

    p {
      cursor: pointer;
    }

    .active {
      color: tomato;
      font-weight: 600;
    }
  }

  .tips {
    position: fixed;
    width: 0.8rem;
    height: 0.6rem;
    background: rgba($color: #000000, $alpha: 0.6);
    color: #fff;
    font-size: 0.3rem;
    text-align: center;
    line-height: 0.6rem;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 0.04rem;
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s linear;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
}
</style>
